package com.angel.controller;

import com.angel.bean.*;
import com.angel.bean.dto.BabyRecordsDto;
import com.angel.dto.RecordsDetilsModel;
import com.angel.service.*;
import com.angel.shiro.SystemAdminAuthorizingRealm;
import com.angel.utils.AdminUserUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/rf")
public class RecordFileController {

    @Autowired
    private BabyInfoService babyInfoService;
    @Autowired
    private ProblemsService problemsService;
    @Autowired
    private BabyRecordsService babyRecordsService;
    @Autowired
    private RecordsDetilsService recordsDetilsService;
    @Autowired
    private OtherlogService otherlogService;

    @RequestMapping("/grf")
    public String getRecordFile(@RequestParam(value = "babyId") String babyId, Model model) {
        int bid = 0;
        try {
            bid = Integer.valueOf(babyId);
        } catch (Exception e) {
            return "index";
        }
        BabyInfo babyInfo = new BabyInfo();
        babyInfo.setBabyId(bid);
        babyInfo = babyInfoService.findOne(babyInfo);
        if (babyId == null) {
            return "index";
        }
        Problems problems = new Problems();
        problems.setStatus(1);
        List<Problems> problemsList = problemsService.selectAll(problems);
        model.addAttribute("problems", problemsList);
        model.addAttribute("baby", babyInfo);
        return "adbabaydrecord";
    }

    @RequestMapping("addrf")
    public String addRecordFile(RecordsDetilsModel recordsDetilsModel) {
        BabyRecords babyRecords = new BabyRecords();
        if (recordsDetilsModel.getRemarks() != null) {
            babyRecords.setRemarks(recordsDetilsModel.getRemarks());
        }
        if (recordsDetilsModel.getBabyid() != null) {
            babyRecords.setBid(recordsDetilsModel.getBabyid());
        }
        SystemAdminAuthorizingRealm.Principal principal = AdminUserUtils.getPrincipal();
        Employee employee = principal.getEmployee();
        babyRecords.setCreateUser(employee.getEmId());
        babyRecords.setSubid(employee.getBranchesId());
        babyRecords.setStatus(1);
        babyRecords.setCreateTime(new Timestamp(System.currentTimeMillis()));
        Map map = babyRecordsService.insertAndDetils(babyRecords, recordsDetilsModel.getRecordsDetils());
        if ((Boolean) map.get("result")) {
            return "404";
        }
        return "redirect:/rf/getbrfl?babyId=" + recordsDetilsModel.getBabyid();
        // return "index";
    }

    @RequestMapping("/getbrfl")
    public String getBabyRecordsList(@RequestParam(value = "babyId") String babyid, @RequestParam(value = "pn", defaultValue = "1") String pn, Model model) {
        int baby = 0;
        try {
            baby = Integer.valueOf(babyid);
        } catch (Exception e) {
            return "index";
        }

        int pageNo = 1;
        try {
            pageNo = Integer.valueOf(pn);
        } catch (Exception e) {
            pageNo = 1;
        }
        PageHelper.startPage(pageNo, 10);
        BabyRecords babyRecords = new BabyRecords();
        babyRecords.setBid(baby);

        BabyInfo babyInfo = new BabyInfo();
        babyInfo.setBabyId(baby);
        babyInfo = babyInfoService.findOne(babyInfo);
        List<BabyRecordsDto> babyRecordsDtos = babyRecordsService.findRcord(babyRecords);
        PageInfo pageInfo = new PageInfo(babyRecordsDtos);
        if (babyRecordsDtos != null && !babyRecordsDtos.isEmpty()) {
            model.addAttribute("pageInfo", pageInfo);
            model.addAttribute("baby", babyInfo);
        }
        return "recordFilesList";
    }

    @RequestMapping("/updatebr")
    @ResponseBody
    public Map update(BabyRecords babyRecords) {
        Map result = new HashMap();
        result.put("success", false);
        if (babyRecords.getId() != null) {
            try {
                babyRecordsService.update(babyRecords);
                result.put("success", true);
                result.put("msg", "更新成功");
            } catch (Exception e) {
                result.put("msg", "数据异常");
            }
        }
        return result;
    }

    @RequestMapping("/getrfbd")
    @ResponseBody
    public Map getRecordFileById(@Param("rfid") String rfid) {
        Map result = new HashMap();
        result.put("success", false);
        Integer id = null;
        try {
            id = Integer.valueOf(rfid);
        } catch (Exception e) {
        }
        result.put("success", false);
       BabyRecords babyRecords = babyRecordsService.findOneRecordByBid(id);
       List<RecordsDetils> detils =null;
       if (babyRecords !=null){
            RecordsDetils recordsDetils = new RecordsDetils();
            recordsDetils.setBrid(babyRecords.getId());
            BabyInfo babyInfo = new BabyInfo();
            babyInfo.setBabyId(babyRecords.getBid());
            babyInfo = babyInfoService.findOne(babyInfo);
            detils = recordsDetilsService.selectedAll(recordsDetils);
            if (detils!=null && !detils.isEmpty()){
                result.put("success", true);
                Map data = new HashMap();
                data.put("record",babyRecords);
                data.put("detils",detils);
                data.put("baby",babyInfo);
                result.put("data",data);
            }
       }
        return result;
    }

}
